package com.SpringBoot.company.service;

import com.SpringBoot.Util.Pager;
import com.SpringBoot.Util.Result;
import com.SpringBoot.company.mapper.CompanyMapper;
import com.SpringBoot.company.pojo.Company;
import com.SpringBoot.user.pojo.User;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bxk
 * @since 2025-09-15
 */
@Service
public class CompanyService {

    private final CompanyMapper companyMapper;

    public CompanyService(CompanyMapper companyMapper) {
        this.companyMapper = companyMapper;
    }

    public Result addCompany(Company company) {
        Company existCompany = companyMapper.getCompanyByName(company.getCompanyName());
        if (existCompany == null) {
            companyMapper.addCompany(company);
            return Result.success("添加成功");
        }else {
            companyMapper.updateCompany(company);
            return Result.success("修改成功");
        }
    }

    public Result updateCompany(Company company) {
        System.out.println(company);
        Company existCompany = companyMapper.getCompanyById(company.getCompanyId());
        System.out.println(existCompany);
        if (existCompany != null) {
            companyMapper.updateCompany(company);
            return Result.success("修改成功");
        }else {
            return Result.error(404,"没有本条信息");
        }
    }

    public Result allCompany(){
        List<Company> companies = companyMapper.allCompany();
        if (companies.size() > 0) {
             return Result.success(companies);
        }else {
            return Result.error(404,"没有赞助公司");
        }
    }

    public Result deleteCompanyById(Integer companyId) {
        Company existCompany = companyMapper.getCompanyById(companyId);
        if (existCompany != null) {
            existCompany.setIsDeleted(1);
            companyMapper.deleteCompanyById(existCompany);
            return Result.success("删除成功");
        }else {
            return Result.error(404,"该公司不存在");
        }
    }

    public Result allCompanyByPage(Integer pageNum, Integer pageSize){
        //查询总记录数
        Integer recordCount = companyMapper.count();
        //构建分页信息对象
        Pager pager = new Pager(pageNum, pageSize, recordCount);
        //根据pager查询需要的数据
        List<Company> list = companyMapper.allCompanyPage(pager.getStart(), pageSize);
        //将分页信息和查询到的数据一起返回到前端
        HashMap<String,Object> map = new HashMap<>();
        map.put("pager",pager);
        map.put("list",list);
        System.out.println(map);
        return Result.success(map);
    }

}
